<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-steps :active="active" align-center>
      <el-step title="基本信息"></el-step>
      <el-step title="地址信息"></el-step> 
    </el-steps> 
    <br/>
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <div v-show="active==1" style="margin-top: 20px;">
        <el-col :span="12">
            <el-form-item label="用户" prop="user.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择用户"
            labelName = 'nikeName'
            labelValue = 'id'
            :value = "inputForm.user.id"
            :limit="1"
            @getValue='(value) => {inputForm.user.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'phone',
              label: '手机号(账号唯一)'
            },
            {
              prop: 'nikeName',
              label: '昵称'
            }
            ]"
            :searchs="[
            {
              prop: 'phone',
              label: '手机号(账号唯一)'
            }
            ]"
            dataListUrl="/userinfo/tUserInfo/list"
            entityBeanName="tUserInfo"
            queryEntityUrl="/userinfo/tUserInfo/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="连锁店" prop="radio"
                :rules="[
                 ]">
              <el-radio-group v-model="radio">
              <el-radio label="1">是</el-radio>
              <el-radio label="2">否</el-radio>
             </el-radio-group>
           </el-form-item>
        </el-col>
        <el-col :span="24" v-if="radio == '1'">
            <el-form-item label="门店" prop="mainStore"
                :rules="[
                 ]">
          <GridSelect
            title="选择门店"
            labelName = 'shopName'
            labelValue = 'id'
            :value = "inputForm.mainStore"
            :limit="1"
            @getValue='(value) => {inputForm.mainStore=value}'
            :columns="[
             
            {
              prop: 'shopName',
              label: '门店名称'
            },
            {
              prop: 'shopAddress',
              label: '办公地址'
            },
            {
              prop: 'shopPhone',
              label: '门店联系电话'
            },
            {
              prop: 'contacts',
              label: '联系人'
            },
            {
              prop: 'contactsPhone',
              label: '联系人电话'
            }
            ]"
            :searchs="[
            {
              prop: 'shopName',
              label: '门店名称'
            },
            {
              prop: 'shopPhone',
              label: '门店联系电话'
            }
            ]"
            dataListUrl="/tshopinfo/tShopInfo/list"
            entityBeanName="tShopInfo"
            queryEntityUrl="/tshopinfo/tShopInfo/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="门店名称" prop="shopName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.shopName" placeholder="请填写门店名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商家类型" prop="shopType"
              :rules="[
               ]">
            <!-- <el-input v-model="inputForm.shopArea" placeholder="请填写办公区域"     ></el-input> -->
            <el-select v-model="inputForm.shopType" placeholder="请选择类型" size="small" style="width: 100%;">
                <el-option  label="店铺" value="1"> </el-option>
                <el-option  label="楼盘" value="2"> </el-option>
             
            </el-select>
         </el-form-item>
      </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contacts"
              :rules="[
               ]">
            <el-input v-model="inputForm.contacts" placeholder="请填写联系人"     ></el-input>
         </el-form-item>
      </el-col>
      <el-col :span="12">
          <el-form-item label="联系人电话" prop="contactsPhone"
              :rules="[
               ]">
            <el-input v-model="inputForm.contactsPhone" placeholder="请填写联系人电话"     ></el-input>
         </el-form-item>
      </el-col>
        <el-col :span="12">
          <el-form-item label="门店联系电话" prop="shopPhone"
              :rules="[
               ]">
            <el-input v-model="inputForm.shopPhone" placeholder="请填写门店联系电话"     ></el-input>
         </el-form-item>
      </el-col>
        <el-col :span="24">
            <el-form-item label="门店介绍" prop="shopIntroduce"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.shopIntroduce" placeholder="请填写门店介绍"     ></el-input>
           </el-form-item>
        </el-col>
        
       
        
        <el-col :span="24">
            <el-form-item label="店铺头像" prop="image"
                :rules="[
                 ]">
              <el-upload ref="image"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tshopinfo/tShopInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => { 
                      showImg(file.url)
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.image = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="2"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 2 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="imageArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="营业执照" prop="businessLicense"
                :rules="[
                 ]">
              <el-upload ref="businessLicense"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tshopinfo/tShopInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                      showImg(file.url)
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.businessLicense = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.businessLicense = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="businessLicenseArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="法人身份证信息面" prop="idCardUp"
                :rules="[
                 ]">
              <el-upload ref="idCardUp"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tshopinfo/tShopInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                      showImg(file.url)
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.idCardUp = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.idCardUp = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="idCardUpArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="法人身份证国徽面" prop="idCardDown"
                :rules="[
                 ]">
              <el-upload ref="idCardDown"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tshopinfo/tShopInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                      showImg(file.url)
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.idCardDown = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.idCardDown = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="idCardDownArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="联系人身份证信息面" prop="idCardUp"
              :rules="[
               ]">
            <el-upload ref="cardUp"
            v-if="visible"
            list-type="picture-card"
                  :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tshopinfo/tShopInfo`"
                  :headers="{token: $cookie.get('token')}"
                  :on-preview="(file, fileList) => {
                    showImg(file.url)
                  }"
                  :on-success="(response, file, fileList) => {
                     inputForm.cardUp = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                  }"
                  :on-remove="(file, fileList) => {
                    $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                      $message.success(data.msg)
                    })
                    inputForm.cardUp = fileList.map(item => item.url).join('|')
                  }"
                  :before-remove="(file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`)
                  }"
                  multiple
                  :limit="1"
                  :on-exceed="(files, fileList) =>{
                    $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                  }"
                  :file-list="cardUpArra">
                  <i class="el-icon-plus"></i>
                </el-upload>
         </el-form-item>
      </el-col>
      <el-col :span="24">
          <el-form-item label="联系人身份证国徽面" prop="cardDown"
              :rules="[
               ]">
            <el-upload ref="cardDown"
            v-if="visible"
            list-type="picture-card"
                  :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tshopinfo/tShopInfo`"
                  :headers="{token: $cookie.get('token')}"
                  :on-preview="(file, fileList) => {
                    showImg(file.url)
                  }"
                  :on-success="(response, file, fileList) => {
                     inputForm.cardDown = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                  }"
                  :on-remove="(file, fileList) => {
                    $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                      $message.success(data.msg)
                    })
                    inputForm.cardDown = fileList.map(item => item.url).join('|')
                  }"
                  :before-remove="(file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`)
                  }"
                  multiple
                  :limit="1"
                  :on-exceed="(files, fileList) =>{
                    $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                  }"
                  :file-list="cardDownArra">
                  <i class="el-icon-plus"></i>
                </el-upload>
         </el-form-item>
      </el-col>
        <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24" v-if="inputForm.status==2">
            <el-form-item label="拒绝原因" prop="jjPerson"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.jjPerson"   ></el-input>
           </el-form-item>
        </el-col>
        </div>
        <div v-show="active==2" style="margin-top: 20px;">
        <el-col :span="12">
          <el-form-item label="办公区域" prop="shopArea"
              :rules="[
               ]">
            <!-- <el-input v-model="inputForm.shopArea" placeholder="请填写办公区域"     ></el-input> -->
            <el-select v-model="inputForm.shopArea" placeholder="请选择类型" size="small" style="width: 100%;">
              <el-option
                v-for="item in valueList"
                :key="item.valueName"
                :label="item.valueName"
                :value="item.valueName">
              </el-option>
            </el-select>
         </el-form-item>
      </el-col>
     
      <el-col :span="24">
          <el-form-item label="办公地址" prop="shopAddress"
              :rules="[
               ]">
            <el-input v-model="inputForm.shopAddress" ref="handleinput" id="handleinput" placeholder="请填写办公地址"     ></el-input>
         </el-form-item>
      </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude"
                :rules="[
                ]">
              <el-input v-model="inputForm.latitude"  disabled placeholder="请填写纬度"     ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="经度" prop="longitude"
                :rules="[
                ]">
              <el-input v-model="inputForm.longitude" disabled placeholder="请填写经度"     ></el-input>
          </el-form-item>
        </el-col>
          <el-col>
            <el-form-item
              label="地图"
              prop=""
            >
              <div
                ref="container"
                id="containepic"
              ></div>
            </el-form-item>
          </el-col>
        </div>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="active !== 1" style="margin-top: 12px;" @click="upper">上一步</el-button>
      <el-button v-if="active !== 2" style="margin-top: 12px;" @click="next">下一步</el-button>
      <el-button v-if="method == 'view' && inputForm.status == 0" style="margin-top: 12px;"  @click="audit(inputForm.id,1)">通过</el-button>
      <el-button v-if="method == 'view' && inputForm.status == 0" style="margin-top: 12px;" @click="jj(inputForm.id,2)">拒绝</el-button>
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>

</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        active: 1,
        visible: false,
        visibles: false,
        loading: false,
        imageArra: [],
        businessLicenseArra: [],
        idCardUpArra: [],
        cardUpArra: [],
        idCardDownArra: [],
        cardDownArra: [],
        valueList: [],
        inputForm: {
          id: '',
          user: {
            id: ''
          },
          shop: {
            id: ''
          },
          shopName: '',
          shopType:'',
          shopIntroduce: '',
          shopArea: '',
          shopAddress: '',
          shopPhone: '',
          contacts: '',
          contactsPhone: '',
          image: '',
          longitude:'',
          latitude:'',
          businessLicense: '',
          idCardUp: '',
          cardUp: '',
          idCardDown: '',
          cardDown: '',
          status: '',
          remarks: '',
          mainStore:''
        },
        markersArray : [],
        url:'',
        urlData:[],
        radio:'2'
      }
      
    },
    components: {
      GridSelect
    },
    activated () {
      this.getvalueList()
    },
    methods: {
      showImg(url) {
      this.$hevueImgPreview(url)
    },
    audit(id,status) { 
        this.$confirm(`确定当前操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/tshopinfo/tShopInfo/audit',
            method: 'post',
            params: {'id': id,"status":status}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.$parent.refreshList()
            }
            // console.log('1111')
            // this.visible = false
            this.loading = false
          })
        })
      },
      jj(id,status){
        this.$prompt('请输入拒绝原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          if (!value){
              this.$message.error("请输入拒绝原因");
              return false
            }
          this.loading = true
          this.$http({
            url: '/tshopinfo/tShopInfo/audit',
            method: 'post',
            params: {'id': id,"status":status,'jjPerson':value}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.$parent.refreshList()
            }
            //  console.log('1111')
            // this.visible = false
            this.loading = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
      },
       // 获取数据列表
       getvalueList () { 
        this.$http({
          url: '/valueinfo/tValueInfo/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 999,
            'orderBy': this.orderBy,
            'type' : 7
          }
        }).then(({data}) => {
          if (data && data.success) { 
            this.valueList = data.page.list 
          }
        })
      },
      initMap (longitude,latitude) {
        var that = this;
        let address = "";
        let lat = "36.752508224779746";
        let logt = "119.97121810913086";
        if (!longitude){
          let lat = latitude;
          let logt =longitude;
        }
        if (this.inputForm.latitude === ''){
          var center = new qq.maps.LatLng(lat,logt);
        } else {
          var center = new qq.maps.LatLng(this.inputForm.latitude,this.inputForm.longitude);
        }
        var map = new qq.maps.Map(document.getElementById("containepic"), {
          center: center,
          zoom: 13,
          disableDefaultUI: true,
        });
        var marker = new qq.maps.Marker({
          position: center,
          map: map,
        });
        that.markersArray.push(marker);
        var infoWin = new qq.maps.InfoWindow({
          map: map,
        }); //此处是点击地图后生成的地标名称
        var geocoder = new qq.maps.Geocoder({
          complete: function (res) {
            that.deleteOverlays();
            map.setCenter(res.detail.location);
            var marker = new qq.maps.Marker({
              map: map,
              position: res.detail.location,
            });
            that.markersArray.push(marker);
          },
        });
  
        qq.maps.event.addListener(map, "click", function (event) {
          that.deleteOverlays();
          // this.latitude = event.latLng.getLat();
          // this.longiistude = event.latLng.getLng();
          // console.log(this.longiistude, this.latitude);
          that.inputForm.longitude = event.latLng.getLng();
          that.inputForm.latitude = event.latLng.getLat();
          var position = new qq.maps.LatLng(that.inputForm.latitude,that.inputForm.longitude);
          var marker = new qq.maps.Marker({
              map: map,
              position: position
            });
          that.markersArray.push(marker);
        });
        document.getElementById("handleinput").addEventListener("blur", function () {
            console.log("1111");
            geocoder.getLocation(that.inputForm.shopAddress);
          });
      },
      deleteOverlays() {
          if (this.markersArray) {
              for (var i in this.markersArray) {
                this.markersArray[i].setMap(null);
              }
              this.markersArray.length = 0;
          }
      },
      init (method, id) {
        this.active = 1
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建店铺`
        } else if (method === 'edit') {
          this.title = '修改店铺'
        } else if (method === 'view') {
          this.title = '查看店铺'
        }
        this.imageArra = []
        this.businessLicenseArra = []
        this.idCardUpArra = []
        this.idCardDownArra = []
          this.cardUpArra = []
        this.cardDownArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/tshopinfo/tShopInfo/queryShopInfoCopy?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              console.log(data,'211231')
              this.inputForm = this.recover(this.inputForm, data.data)
               if (this.inputForm.mainStore !='' && this.inputForm.mainStore != 0) {
                this.radio='1'
              }
              this.initMap(this.inputForm.longitude,this.inputForm.latitude);
              this.inputForm.image.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.imageArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.businessLicense.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.businessLicenseArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.idCardUp.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.idCardUpArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.idCardDown.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.idCardDownArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.cardUp.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.cardUpArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.cardDown.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.cardDownArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          } else {
              this.initMap();
          }
        })
      },
      next() { 
          this.active++ 
          if (this.active==1){
            console.log(123213213213);
            // this.initMap();
          }
        },
        upper() {
          this.active--
          if (this.active==1){
            console.log(123213213213);
            // this.initMap();
          }
        },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/tshopinfo/tShopInfo/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="less">
  #containepic {
    width: 750px;
    height: 500px;
  }
  .showPic .el-message-box__message{
    width: 800px;
    height: 800px;
    overflow: hidden;
    display: flex;
     p img {
      height: 100%!important;
      width: 100%!important;
      object-fit: contain;
    }
  }
  .imgstyle{
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  </style>